package a06_栈与队列;

import java.util.Stack;

/**
 * <p>
 * a02_用栈实现队列复习4
 * </p>
 *
 * @author flyduck
 * @since 2025/5/15
 */
public class a02_用栈实现队列复习4 {
}

class MyQueue5 {

    private Stack<Integer> inStack;
    private Stack<Integer> outStack;

    public MyQueue5() {
        inStack = new Stack<>();
        outStack = new Stack<>();
    }

    public void push(int x) {
        inStack.add(x);
    }

    public int pop() {
        if (!outStack.isEmpty()) {
            return outStack.pop();
        }else {
            while (!inStack.isEmpty()){
                outStack.add(inStack.pop());
            }
            return outStack.pop();
        }
    }

    public int peek() {
        if (!outStack.isEmpty()) {
            return outStack.peek();
        }else {
            while (!inStack.isEmpty()){
                outStack.add(inStack.pop());
            }
            return outStack.peek();
        }
    }

    public boolean empty() {
        return inStack.isEmpty() && outStack.isEmpty();
    }
}
